# -*- python -*-
# ex: set syntax=python:

# Copyright 2014-2019 GSI, Inc. All rights reserved.
import os
import tempfile

from buildbot.plugins import *

# This is a buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory

PASSWORD = open('/Users/anar/.passwd_buildbot', 'rb').readlines()[0].strip()

repo_url = "https://github.com/FairRootGroup/DDS";

# This is the dictionary that the buildmaster pays attention to.
c = BuildmasterConfig = {}
#----------------------------------------------------------------------
####### WORKERS
#----------------------------------------------------------------------
c['workers'] = [worker.Worker("CentOS7", PASSWORD),
               worker.Worker("Ubuntu", PASSWORD),
               worker.Worker("OSX_WN_pkg", PASSWORD),
	           worker.Worker("OSX", PASSWORD)]

c['protocols'] = {'pb': {'port': 9999}}

#----------------------------------------------------------------------
####### CHANGESOURCES
#----------------------------------------------------------------------
c['change_source'] = []
#c['change_source'].append(changes.GitPoller(
#        'https://github.com/buildbot/hello-world.git',  # the buildbot clone of pyflakes
#        workdir='gitpoller-workdir', branch='master',
#        pollinterval=300))


#----------------------------------------------------------------------
####### SCHEDULERS
#----------------------------------------------------------------------
c['schedulers'] = []
sch_wn_pkg = schedulers.Nightly(name="sch_wn_pkg",
                     hour=1, minute=0,
                     branch='master',
                     builderNames=["Wn-Pkg-centos7", "Wn-Pkg-OSX10"])

full_build = schedulers.Dependent(name="full_build",
                      upstream=sch_wn_pkg,
                      builderNames=["RELEASE"])

c['schedulers'] = [sch_wn_pkg, full_build]


c['schedulers'].append(schedulers.ForceScheduler(
                        name="force",
                        builderNames=["RELEASE", "Wn-Pkg-centos7", "Wn-Pkg-OSX10"]))
                        
#----------------------------------------------------------------------
####### BUILDERS
#----------------------------------------------------------------------
builders = []

#----------------------------------------------------------------------
# ---> Custom Environment
#----------------------------------------------------------------------
env_linux_boost = {
         'BOOST_ROOT':'/root/Development',
         'LD_LIBRARY_PATH':'/root/Development/lib'
}

env_macos_boost = {
         'BOOST_ROOT':'/Users/anar/Development/BOOST',
         'DYLD_LIBRARY_PATH':'/Users/anar/Development/BOOST/lib'
}


#----------------------------------------------------------------------
# ---> Building DDS
#----------------------------------------------------------------------

f_DDS = util.BuildFactory()
f_DDS.addStep(steps.Git(mode='full', method='clobber', submodules=True,
                  repourl=repo_url,
                  haltOnFailure=True))

f_DDS.addStep(steps.ShellCommand(command="mkdir build",
                  description=["mkdir build"],
                  haltOnFailure=1))

basedir = os.path.realpath(tempfile.mktemp())
os.mkdir(basedir)
        
f_DDS.addStep(steps.ShellCommand (command="mkdir build; cmake -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX:PATH="+ basedir +" -C ../BuildSetup.cmake ..",
	          workdir="build/build",
		  description=["configure"],
                  haltOnFailure=1))

f_DDS.addStep(steps.Compile(command=["make", "-j4"],
		   flunkOnWarnings=1,
                   haltOnFailure=1,
                   workdir="build/build"))

f_DDS.addStep(steps.ShellCommand (command="make install",
		  workdir="build/build",
                  description=["test install"],
                  haltOnFailure=1))

f_DDS.addStep(steps.ShellCommand (command="./run_test.sh /tmp/POD_INSTALL",
                  workdir="/tmp/POD_INSTALL/tests",
                  description=["run tests"],
                  haltOnFailure=1))

f_DDS.addStep(steps.ShellCommand (command="rm -rf "+basedir,
                  description=["cleaning"],
                  haltOnFailure=1))

#----------------------------------------------------------------------
# ---> Building DDS_full
#----------------------------------------------------------------------
f_DDS_full = util.BuildFactory()
f_DDS_full.addStep(steps.Git(mode='full', method='clobber', submodules=True,
                       repourl=repo_url,
                       haltOnFailure=True))

f_DDS_full.addStep(steps.ShellCommand (command="mkdir build",
                  description=["mkdir build"],
                  haltOnFailure=1))
                  
basedir = os.path.realpath(tempfile.mktemp())
os.mkdir(basedir)

f_DDS_full.addStep(steps.ShellCommand (command="cmake -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX:PATH=" + basedir + " -C ../BuildSetup.cmake ..",
                  workdir="build/build",
                  description=["configure"],
                  haltOnFailure=1))

f_DDS_full.addStep(steps.Compile(command=["make", "-j4"],
		   flunkOnWarnings=1,
                   haltOnFailure=1,
                   workdir="build/build"))

f_DDS_full.addStep(steps.ShellCommand (command="make install",
                  workdir="build/build",
                  description=["test install"],
                  haltOnFailure=1))
                  
f_DDS_full.addStep(steps.ShellCommand (command="make api-docs",
                    workdir="build/build",
                    description=["build API docs"],
                    haltOnFailure=1))
                    
f_DDS_full.addStep(steps.ShellCommand (command="make upload-api-docs",
                    workdir="build/build",
                    description=["upload API docs"],
                    haltOnFailure=1))

f_DDS_full.addStep(steps.ShellCommand (command="./run_test.sh " + basedir,
                  workdir= basedir + "/tests",
                  description=["run tests"],
                  haltOnFailure=1))
                          
f_DDS_full.addStep(steps.ShellCommand(command="make package_source", 
                                      workdir="build/build",
                                      description=["build src package"],
                                      haltOnFailure=1))

f_DDS_full.addStep(steps.ShellCommand(command="chmod go+xr *.tar.gz; scp -o StrictHostKeyChecking=no -o PasswordAuthentication=no -p *.tar.gz ddswww@lxi001:/u/ddswww/web-docs/releases/dds/nightly",
                                      workdir="build/build",
                                      description=["release tarball"],
                                      haltOnFailure=1))

f_DDS_full.addStep(steps.ShellCommand (command="rm -rf " + basedir,
                  description=["cleaning"],
                  haltOnFailure=1))

#----------------------------------------------------------------------
# ---> Building DDS WN packages
#----------------------------------------------------------------------
f_DDS_wn_pkg = util.BuildFactory()
f_DDS_wn_pkg.addStep(steps.Git(mode='full', method='clobber', submodules=True,
                     repourl=repo_url,
                     haltOnFailure=True))
                     
basedir = os.path.realpath(tempfile.mktemp())
os.mkdir(basedir)

f_DDS_wn_pkg.addStep(steps.ShellCommand (command="cmake -DCMAKE_OSX_ARCHITECTURES=\"x86_64\" -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX:PATH="+ basedir + " -C ../BuildSetup.cmake ..",
                  workdir="build/build",
                  description=["configure"],
                  haltOnFailure=1))

f_DDS_wn_pkg.addStep(steps.Compile(command=["make", "-j4", "wn_bin"],
		           flunkOnWarnings=1,
                   haltOnFailure=1,
                   workdir="build/build"))

f_DDS_wn_pkg.addStep(steps.ShellCommand (command="make -j4 install",
                  workdir="build/build",
                  description=["test install"],
                  haltOnFailure=1))

f_DDS_wn_pkg.addStep(steps.ShellCommand (command="./run_test.sh "+ basedir,
                  workdir= basedir + "/tests",
                  description=["run tests"],
                  haltOnFailure=1))

#f_DDS_wn_pkg.addStep(ShellCommand (command="./dds-octopus-start /tmp/DDS_INSTALL",
#                  workdir="/tmp/DDS_INSTALL/tests/dds-octopus",
#                  description=["DDS Octopus"],
#                  haltOnFailure=1))

f_DDS_wn_pkg.addStep(steps.Compile(command=["make", "wn_bin_upload"],
		           flunkOnWarnings=1,
                   haltOnFailure=1,
                   description=["build WN pkg"],
                   workdir="build/build"))

f_DDS_wn_pkg.addStep(steps.ShellCommand (command="rm -rf " + basedir,
                  description=["cleaning"],
                  haltOnFailure=1))

#----------------------------------------------------------------------
# ---> Building DDS continuous build -- ANAR
#----------------------------------------------------------------------

f_DDS_continuous_anar = util.BuildFactory()
f_DDS_continuous_anar.addStep(steps.Git(mode='full', method='clobber', submodules=True,
                       repourl='https://github.com/AnarManafov/DDS.git',
                       haltOnFailure=True))

f_DDS_continuous_anar.addStep(steps.ShellCommand (command="mkdir build",
                  description=["mkdir build"],
                  haltOnFailure=1))
                  
basedir = os.path.realpath(tempfile.mktemp())
os.mkdir(basedir)

f_DDS_continuous_anar.addStep(steps.ShellCommand (command="cmake -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX:PATH="+ basedir + " -C ../BuildSetup.cmake ..",
                  workdir="build/build",
                  description=["configure"],
                  haltOnFailure=1))

f_DDS_continuous_anar.addStep(steps.Compile(command=["make", "-j4"],
		   flunkOnWarnings=1,
                   haltOnFailure=1,
                   workdir="build/build"))

f_DDS_continuous_anar.addStep(steps.ShellCommand (command="make install",
                  workdir="build/build",
                  description=["test install"],
                  haltOnFailure=1))

f_DDS_continuous_anar.addStep(steps.ShellCommand (command="./run_test.sh " + basedir,
                  workdir= basedir + "/tests",
                  description=["run tests"],
                  haltOnFailure=1))

f_DDS_continuous_anar.addStep(steps.ShellCommand (command="rm -rf " + basedir,
                  description=["cleaning"],
                  haltOnFailure=1))

#----------------------------------------------------------------------
# ---> Building DDS continuous build -- ANDREY
#----------------------------------------------------------------------

f_DDS_continuous_andrey = util.BuildFactory()
f_DDS_continuous_andrey.addStep(steps.Git(mode='full', method='clobber', submodules=True,
                       repourl='https://github.com/AndreyLebedev/DDS.git',
                       haltOnFailure=True))

f_DDS_continuous_andrey.addStep(steps.ShellCommand (command="mkdir build",
                  description=["mkdir build"],
                  haltOnFailure=1))
                  
basedir = os.path.realpath(tempfile.mktemp())
os.mkdir(basedir)

f_DDS_continuous_andrey.addStep(steps.ShellCommand (command="cmake -DBUILD_TESTS=ON -DCMAKE_INSTALL_PREFIX:PATH=" + basedir + " -C ../BuildSetup.cmake ..",
                  workdir="build/build",
                  description=["configure"],
                  haltOnFailure=1))

f_DDS_continuous_andrey.addStep(steps.Compile(command=["make", "-j4"],
		   flunkOnWarnings=1,
                   haltOnFailure=1,
                   workdir="build/build"))

f_DDS_continuous_andrey.addStep(steps.ShellCommand (command="make install",
                  workdir="build/build",
                  description=["test install"],
                  haltOnFailure=1))

f_DDS_continuous_andrey.addStep(steps.ShellCommand (command="./run_test.sh " + basedir,
                  workdir= basedir + "/tests",
                  description=["run tests"],
                  haltOnFailure=1))

f_DDS_continuous_andrey.addStep(steps.ShellCommand (command="rm -rf " + basedir,
                  description=["cleaning"],
                  haltOnFailure=1))


#----------------------------------------------------------------------
# ---> Building DDS-user-manual
#----------------------------------------------------------------------
f_DDSUserManual = util.BuildFactory()
f_DDSUserManual.addStep(steps.Git(repourl='git@github.com:FairRootGroup/DDS-user-manual.git',
                     haltOnFailure=True))

f_DDSUserManual.addStep(steps.ShellCommand (command="make clean",
                     workdir="build/src",
                     description=["clean"],
                     haltOnFailure=1))

f_DDSUserManual.addStep(steps.Compile(command=["make"],
		     workdir="build/src",
		     flunkOnWarnings=1,
                     haltOnFailure=1))

f_DDSUserManual.addStep(steps.ShellCommand (command="chmod -R go+xr ../output; make sync",
 		     workdir="build/src",
                     description=["publish"],
                     haltOnFailure=1))


#----------------------------------------------------------------------
# ---> Building DDS-web-site
#----------------------------------------------------------------------
f_DDSWebsite = util.BuildFactory()
f_DDSWebsite.addStep(steps.Git(repourl='git@github.com:FairRootGroup/DDS-web-site.git',
                     haltOnFailure=True))

f_DDSWebsite.addStep(steps.ShellCommand (command="make clean-all",
                     description=["clean"],
                     haltOnFailure=1))

f_DDSWebsite.addStep(steps.Compile(command=["make"],
		   flunkOnWarnings=1,
                   haltOnFailure=1))

f_DDSWebsite.addStep(steps.ShellCommand (command="chmod -R go+xr html_out; make sync",
                     description=["publish"],
                     haltOnFailure=1))
#----------------------------------------------------------------------
# -------------------------------------------------------------------
c['builders'] = [
                 util.BuilderConfig(name="WEB Site", workernames="OSX", factory=f_DDSWebsite),
                 util.BuilderConfig(name="Doc", workernames="OSX", factory=f_DDSUserManual),
                 util.BuilderConfig(name="Wn-Pkg-centos7", workernames="CentOS7", factory=f_DDS_wn_pkg, env=env_linux_boost),
		 
		         util.BuilderConfig(name="continuous-OSX-ANAR", workernames="OSX", factory=f_DDS_continuous_anar, env=env_macos_boost),
                 util.BuilderConfig(name="continuous-centos7-ANAR", workernames="CentOS7", factory=f_DDS_continuous_anar, env=env_linux_boost),
                 util.BuilderConfig(name="continuous-ubuntu17.04-ANAR", workernames="Ubuntu", factory=f_DDS_continuous_anar),
		 
		         util.BuilderConfig(name="continuous-OSX-ANDREY", workernames="OSX", factory=f_DDS_continuous_andrey, env=env_macos_boost),
                 util.BuilderConfig(name="continuous-centos7-ANDREY", workernames="CentOS7", factory=f_DDS_continuous_andrey, env=env_linux_boost),
                 util.BuilderConfig(name="continuous-ubuntu17.04-ANDREY", workernames="Ubuntu", factory=f_DDS_continuous_anar),
                 
                 util.BuilderConfig(name="Wn-Pkg-OSX10", workernames="OSX", factory=f_DDS_wn_pkg, env=env_macos_boost),
                 util.BuilderConfig(name="RELEASE", workernames="OSX", factory=f_DDS_full, env=env_macos_boost)
]



# ------------> DDS WEbsite <----------------
DDSWebSite_schedule = schedulers.SingleBranchScheduler(name="DDSWebsite", change_filter=util.ChangeFilter(project = 'FairRootGroup/DDS-web-site'), treeStableTimer=180, builderNames=["WEB Site"])
c['schedulers'].append(DDSWebSite_schedule)

DDSUserManual_schedule = schedulers.SingleBranchScheduler(name="DDSUserManual", change_filter=util.ChangeFilter(project = 'FairRootGroup/DDS-user-manual'), treeStableTimer=180, builderNames=["Doc"])
c['schedulers'].append(DDSUserManual_schedule)


DDS_continuous_ANAR_schedule = schedulers.SingleBranchScheduler(name="DDS-continuous_ANAR", change_filter=util.ChangeFilter(project = 'AnarManafov/DDS'), treeStableTimer=180,
                                                builderNames=["continuous-OSX-ANAR",
                                                              "continuous-centos7-ANAR",
                                                              "continuous-ubuntu17.04-ANAR"])
c['schedulers'].append(DDS_continuous_ANAR_schedule)


DDS_continuous_ANDREY_schedule = schedulers.SingleBranchScheduler(name="DDS-continuous_ANDREY", change_filter=util.ChangeFilter(project = 'AndreyLebedev/DDS'), treeStableTimer=180,
                                                builderNames=["continuous-OSX-ANDREY",
                                                              "continuous-centos7-ANDREY",
                                                              "continuous-ubuntu17.04-ANDREY"])
c['schedulers'].append(DDS_continuous_ANDREY_schedule)

#----------------------------------------------------------------------
####### STATUS TARGETS
#----------------------------------------------------------------------
from twisted.cred import strcred

c['www'] = dict(port=22001,
                auth=util.HTPasswdAuth("/Users/anar/Development/buildbot/master/.htpasswd"),
#                allowed_origins=["*"],
                debug=False,
                avatar_methods=[],
                plugins=dict(waterfall_view={}, console_view={}, grid_view={}),
#                change_hook_auth=[strcred.makeChecker("file:changehook.passwd")],
                change_hook_dialects={'github': {'secret': 'A_1258d'}},
                custom_templates_dir = "/Users/anar/Development/DDS-buildbot/master/templates/")

authz = util.Authz(
  allowRules=[
    util.AnyControlEndpointMatcher(role="admins"),
  ],
  roleMatchers=[
    util.RolesFromUsername(roles=["admins"], usernames=["anar"])
  ]
)
c['www']['authz'] = authz


c['services'] = []

m = reporters.MailNotifier(fromaddr="anar@localhost",
                           extraRecipients=["Anar.Manafov@gmail.com", "Andrey.Lebedev@gsi.de"],
                           sendToInterestedUsers=False)
                           
#c['status'] = []

#auth = (util.HTPasswdAuth('/Users/anar/Development/buildbot/master/.htpasswd'))

#authz = util.Authz(auth=auth,
#              forceBuild='auth',
#              stopBuild='auth')

#c['status'].append(WebStatus(http_port=22001, authz=authz,
#                             change_hook_dialects={ 'github' : { } }))

#from buildbot.status import mail
#c['status'].append(mail.MailNotifier(fromaddr="anar@localhost",
#                                     extraRecipients=["Anar.Manafov@gmail.com", "Andrey.Lebedev@gsi.de"],
#                                     sendToInterestedUsers=False,
#				     mode="failing"))

#----------------------------------------------------------------------
####### PROJECT IDENTITY
#----------------------------------------------------------------------
c['projectName'] = "DDS - The Dynamic Deployment System"
c['projectURL'] = "http://dds.gsi.de"
c['buildbotURL'] = "http://demac012.gsi.de:22001/"
